<template>
<!--  <el-button type="primary" @click="add">新增字典类型</el-button>
  <el-button type="danger" @click="batchDel">批量删除</el-button>

  <el-table
      :data="dicTypeList"
      style="width: 100%"
      @selection-change="handleSelectionChange">

    <el-table-column type="selection" width="70"/>
    <el-table-column type="index" label="序号" width="200"/>
    <el-table-column property="typeCode" label="字典类型代码" />
    <el-table-column property="typeName" label="字典类型名称"  show-overflow-tooltip/>
    <el-table-column property="remark" label="备注" width="150"/>
    <el-table-column label="操作" width="230">
      <template #default="scope">
        <el-button type="success" @click="edit(scope.row.id)">编辑</el-button>
        <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-pagination
      background
      layout="prev, pager, next"
      :page-size="pageSize"
      :total="total"
      @prev-click="toPage"
      @next-click="toPage"
      @current-change="toPage"/>-->

  <div>
    <el-button type="primary" :style="{ backgroundColor: '#409EFF', borderColor: '#409EFF' }" @click="add">新增字典类型</el-button>
    <el-button type="danger" :style="{ backgroundColor: '#F56C6C', borderColor: '#F56C6C' }" @click="batchDel">批量删除</el-button>

    <el-table
        :data="dicTypeList"        style="width: 100%; border: 1px solid #DCDFE6;"
        stripe
        @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="70"/>
      <el-table-column type="index" label="序号" width="200"/>
      <el-table-column property="typeCode" label="字典类型代码" :style="{ color: '#409EFF' }" />
      <el-table-column property="typeName" label="字典类型名称" :style="{ color: '#409EFF' }" show-overflow-tooltip/>
      <el-table-column property="remark" label="备注" width="150"/>
      <el-table-column label="操作" width="230">
        <template #default="scope">
          <el-button type="success" :style="{ backgroundColor: '#67C23A', borderColor: '#67C23A' }" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" :style="{ backgroundColor: '#F56C6C', borderColor: '#F56C6C' }" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total"
        @prev-click="toPage"
        @next-click="toPage"
        @current-change="toPage"/>
  </div>

  <!--  新增字典类型的弹窗（对话框）-->
  <el-dialog v-model="dicTypeDialogVisible" :title="dicTypeQuery.id > 0 ? '编辑字典类型' : '新增字典类型'" center width="55%"
             draggable>
    <el-form ref="dicTypeRefFrom" :model="dicTypeQuery" label-width="130px" :rules="dicTypeRules">
      <el-form-item label="字典类型代码" prop="typeCode">
        <el-input v-model="dicTypeQuery.typeCode"/>
      </el-form-item>

      <el-form-item label="字典类型名称" prop="typeName">
        <el-input v-model="dicTypeQuery.typeName"/>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="dicTypeQuery.remark"/>
      </el-form-item>

    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dicTypeDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="dicTypeSubmit">提 交</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import {doDelete, doGet, doPost, doPut} from "../http/httpRequest.js";
import {messageConfirm, messageTip} from "../util/util.js";

export default {
  name: "DicTypeView",

  //注入
  inject: ['reload'],

  data() {
    return {
      dicTypeList: [{}], //字典类型列表
      pageSize: 0, //每页显示的条数
      total: 0, //总条数
      dicTypeIdArray: [], //勾选或者取消勾选的数据会传给我们这个函数

      //字典类型表单对象，初始值是空
      dicTypeQuery: {},
      //定义录入字典类型验证规则
      dicTypeRules: {
        typeCode: [
          {required: true, message: '请输入字典类型代码', trigger: 'blur'},
        ],
        typeName: [
          {required: true, message: '请输入字典类型名称', trigger: 'blur'},
        ],
      },
      ////新增或编辑字典类型的弹窗是否显示
      dicTypeDialogVisible: false,

    }
  },

  mounted() {
    this.getData(1);
  },

  methods: {
    //勾选或者取消勾选时，触发该函数（已经勾选的数据会传给我们这个函数）
    handleSelectionChange(selectionDataArray) {
      this.dicTypeIdArray = [];//清空一下之前选中的数据，只需要最后一次的选中的数据
      selectionDataArray.forEach(data=>{
        let dicTypeId = data.id;
        this.dicTypeIdArray.push(dicTypeId);
      })
    },

    //查询字典类型列表
    getData(current) {
      doGet("/api/dicType", {
        current: current //当前查询第几页
      }).then(resp => {
        if (resp.data.code === 200) {
          this.dicTypeList = resp.data.data.list;
          this.pageSize = resp.data.data.pageSize;
          this.total = resp.data.data.total;
        }
      })
    },

    //分页函数（current是element-plus组件传过来，就是传的当前页）
    toPage(current) {
      this.getData(current);
    },

    //新增字典类型
    add() {
      this.dicTypeQuery = {};
      this.dicTypeDialogVisible = true;
    },

    //编辑字典类型
    edit(id) {
      this.dicTypeDialogVisible = true;
      this.loadDicType(id);
    },

    //加载要编辑的字典类型
    loadDicType(id) {
      doGet("/api/dicType/detail/" + id, {}).then(resp => {
        if (resp.data.code === 200) {
          this.dicTypeQuery = resp.data.data;
        }
      })
    },

    //新增字典类型提交保存
    dicTypeSubmit() {
      let formData = new FormData();
      for (let field in this.dicTypeQuery) {
        if (this.dicTypeQuery[field]){//this.dicTypeQuery[field]字段有值，不是空，才拼接传后端
          formData.append(field, this.dicTypeQuery[field]);
        }
      }
      this.$refs.dicTypeRefFrom.validate((isValid) => {
        if (isValid) {
          if (this.dicTypeQuery.id > 0) { /*编辑*/
            doPut("/api/dicType", formData).then(resp => {
              if (resp.data.code === 200) {
                messageTip("编辑成功", "success");
                //页面刷新
                this.reload();
              } else {
                messageTip("编辑失败", "error");
              }
            })
          } else { /*新增*/
            doPost("/api/dicType", formData).then(resp => {
              if (resp.data.code === 200) {
                messageTip("提交成功", "success");
                //页面刷新
                this.reload();
              } else {
                messageTip("提交失败", "error");
              }
            })
          }
        }
      })
    },

    //删除字典类型
    del(id) {
      messageConfirm("您确定要删除该数据吗？").then(() => {//用户点击“确定”按钮就会触发then函数
        doDelete("/api/dicType/" + id, {}).then(resp => {
          if (resp.data.code === 200) {
            messageTip("删除成功", "success");
            //页面刷新
            this.reload();
          } else {
            messageTip("删除失败，原因：" + resp.data.msg, "error");
          }
        })
      }).catch(() => {//用户点击“取消”按钮就会触发catch函数
        messageTip("取消删除", "warning");
      })
    },

    //批量删除字典类型
    batchDel(){
      if (this.dicTypeIdArray.length <= 0){
        //提示一下
        messageTip("请选择要删除的数据", "warning");
        return;
      }
      messageConfirm("您确定要删除这些数据吗？").then(() => {//用户点击“确定”按钮就会触发then函数
        //原来是数组：[1,3,5,7] --> ids = "1,3,5,7";
        let ids = this.dicTypeIdArray.join(",");
        doDelete("/api/dicType", {ids: ids}).then(resp => {
          if (resp.data.code === 200) {
            messageTip("批量删除成功", "success");
            //页面刷新
            this.reload();
          } else {
            messageTip("批量删除失败，原因：" + resp.data.msg, "error");
          }
        })
      }).catch(() => {//用户点击“取消”按钮就会触发catch函数
        messageTip("取消批量删除", "warning");
      })
    },


  }


}
</script>

<style scoped>

</style>